# @author:Wei Junjie
# @time:2024/6/15 10:36
# @file log_info.py
# --*--coding: utf-8 --*--
"""
日志模块
"""
import logging
import os
import time
import colorlog

_instance=None
class LogInfo:

    def create_logger(self):
        # logger信息
        logger = logging.getLogger()

        logger.setLevel(logging.DEBUG)


        if len(logger.handlers)==0:
            # 创建console对象
            console_handle=logging.StreamHandler()

            console_handle.setLevel(logging.DEBUG)

            formatter = colorlog.formatter.ColoredFormatter("%(log_color)s[%(asctime)s] [%(levelname)s] %(filename)s->[%(module)s.%(funcName)s line:%(lineno)d]: %(message)s", datefmt=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),log_colors={
                'DEBUG': 'cyan'
                , 'INFO': 'green'
                , 'WARNING': 'yellow'
                , 'ERROR': 'red'
                , 'CRITICAL': 'red'
            })
            console_handle.setFormatter(formatter)

            # 创建文件对象
            log_file_dir=os.path.join(os.getcwd(),'log_information')

            os.makedirs(log_file_dir, exist_ok=True)

            log_file_path=os.path.join(log_file_dir,time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime())+'.log')


            file_handle=logging.FileHandler(log_file_path)

            file_handle.setLevel(logging.DEBUG)

            file_handle.setFormatter(formatter)

            # 添加终端console对象到log中
            logger.addHandler(console_handle)
            # 添加文件对象到log中
            logger.addHandler(file_handle)

            return logger

log_info=LogInfo().create_logger()


if __name__ == '__main__':
    pass
